#include "Calender.h"

Calender::Calender(){}

bool Calender::init(){
	for(int i=0;i<7;i++){
		Day* d = new Day();

		if (d==NULL){
			for(i--;i>=0;i--){
				delete(days[i]);
			}
			return false;
		}

		this->days[i] = d;
	}
	return true;
}

Calender::~Calender(void){
	this->freeCalenderMem();
}

Calender::Calender(const Calender& calender){
	for(int i=0;i<7;i++){
		this->days[i] = new Day(*calender.days[i]);
		if (this->days[i]==NULL){
			for(i--;i>=0;i--){
				delete(days[i]);
			}
			return;
		}
	}
}

const Calender& Calender::operator =(const Calender& calender){
	if (&calender != this)
	{
		this->freeCalenderMem(); //free meomry from prev values
	
		for(int i=0;i<7;i++){
			this->days[i] = new Day(*calender.days[i]);
			if (this->days[i]==NULL){
			for(i--;i>=0;i--){
				delete(days[i]);
			}
			return *this;
		}
		}
	}
	return *this;
}

void Calender::freeCalenderMem(){
	for(int i=0;i<7;i++){
			delete(this->days[i]); //free meomry from prev values
	}
}

void Calender::cleanCalender(){
	for(int i=0;i<7;i++){
		this->days[i]->cleanDay(); //free meomry from prev values
	}
}


	// set a new appointment in the calender
	// return true iff the operation passed
bool Calender::addMeeting(int dayNum, float startTime, float endTime, string& subject){
	if (legalTimeFormat(startTime) &&
		legalTimeFormat(endTime) &&
		legalDay(dayNum) && 
		(startTime<=endTime)){

		Meeting* m = new Meeting(startTime,endTime,subject);
		if (m == NULL){
			return false;
		}
		bool flag = this->days[dayNum]->addMeeting(*m);

		if (!flag){
			delete(m);
		}

		return flag;

	} else {
		return false;
	}
}

	// remove appointment from the calender
	// return true iff the operation passed
bool Calender::removeMeeting(const int day,const float startTime){ 
	if (legalDay(day) && legalTimeFormat(startTime))
	{
		Meeting * m = days[day]->getMeeting(startTime);
		if (m==NULL)
			return false;

		return days[day]->removeMeeting(*m);

	} else
		return false;

}

	// get appointmet by start time and day
	const Meeting* Calender::getMeetingByStartTime(int day, float startTime) const{
		if (!legalDay(day) || !legalTimeFormat(startTime))
			return NULL;
		else
		{
			const Meeting * m = days[day]->getMeeting(startTime);
			return m;
		}
	}

	// set the meeting
	// return true iff the operation passed
	bool Calender::copyMeeting(const Meeting& oldm, int newDay, float newTime){
		if (legalDay(newDay) && legalTimeFormat(newTime) &&
			(newTime + oldm.getTimeEnd()-oldm.getTimeStart()) <= 24 ){

			Meeting * m = new Meeting(oldm);
			if (m == NULL) return false;
			m->setTimeStart(newTime);
			m->setTimeEnd(newTime + oldm.getTimeEnd()-oldm.getTimeStart());
			
			bool flag = days[newDay]->addMeeting(*m);
			if (!flag)
				delete(m);
			return flag;

		} else {
			return false;
		}

	}

	// return a string representation of the calender
	ostream& Calender::writeToStream(ostream & str) const{
		for(int i=0;i<7;i++){
			str << "Day: " << i+1 << endl;
			days[i]->toString(str);
			str << endl;
		}
		return str;
	}
 

	


	//helper functions-
	bool Calender::legalDay(const int d) const{
		return ((d>=0) && (d<7));
	}

	bool Calender::legalTimeFormat(const float t) const{
		return ((t >= 0) && (t < 24));
	}